import {
  config
} from "../config.js";

const tips = {
  1005: "appkey无效,请前往七月网站获取",
  3000: "期刊不存在",
  1: "抱歉,出现了一个错误!"
}


export class HTTP {

  request(params) {
    if (!params.method) {
      params.method = "GET";
    }
    wx.request({
      url: config.api_base_url + params.url,
      method: params.method,
      data: params.data,
      header: {
        "content-type": "application/json",
        "appkey": config.appkey
      },
      success: (res) => {
        let code = res.statusCode.toString();
        if (code.startsWith("2")) {
          params.success && params.success(res);
        } else {
          let error_code = res.data.error_code;
          this._showError(error_code);
        }
      },
      fail: (error) => {
        this._showError();
      }
    })
  }

  _showError(error_code) {
    if (!error_code || (error_code != 1005 || error_code != 3000)) {
      error_code = 1;
    }
    wx.showToast({
      title: tips[error_code],
      icon: "none",
      duration: 1500
    })
  }

}